<?php

/*
 * Vérification des droits dans l'application
 */

class droitsFilter extends sfFilter {
  
  public function execute($filterChain) { 
    $redirection = "salarie/index";
    $user = $this->getContext()->getUser();
    
    try {
      if($user->isAuthenticated() && !$user->isSuperAdmin()) {
        $request = $this->getContext()->getRequest();
        
        switch ($request->getParameter('module')) {
          case 'salarie' :
            switch ($request->getParameter('action')) {
              case 'index':
                return $filterChain->execute();
              break;
              
              default :
                return $filterChain->execute();
              break;
            }
          break;
        
          case 'calendrier' :
            if(!$request->hasParameter('id')) {
              $user->setFlash('notice', "Calendrier: Vous devez spécifier un identifiant de salarié !");
              return $this->getContext()->getController()->redirect($redirection);
            }

            $salarie = SalariePeer::retrieveByPK($request->getParameter('id'));
            if(!$salarie instanceof Salarie) {
              $user->setFlash('notice', "Calendrier: Aucun salarié trouvé pour l'identifiant \"" . $request->getParameter('id') . "\" !");
              return $this->getContext()->getController()->redirect($redirection);
            }
            
            switch($request->getParameter('action')) {
              case 'index':
              case 'submit':
                $uf_id = $request->hasParameter('uf') ? $request->getParameter('uf') : null;
                if($user->isSuperAdmin() || $user->droitModificationCalendrier($salarie, $uf_id))
                  return $filterChain->execute();
                elseif($user->hasPermission('view_all'))
                  $this->getContext()->getController()->redirect('calendrier/view?id=' . $salarie->getPrimaryKey()
                    . '&df_day=' . $request->getParameter('df_day')
                    . '&df_month=' . $request->getParameter('df_month')
                    . '&df_year=' . $request->getParameter('df_year')
                  );
                else
                  return $this->getContext()->getController()->redirect($redirection);
              break;
              
              case 'view':
                if($user->isSuperAdmin() || $user->droitConsultationCalendrier($salarie))
                  return $filterChain->execute();
                else
                  return $this->getContext()->getController()->redirect($redirection);
              break;
              
              default :
                return $filterChain->execute();
              break;
            }
          break;
        
          default :
            return $filterChain->execute();
          break;
        }
      } else
        return $filterChain->execute();
    } catch(sfException $e) {
        throw $e;
//      $this->getContext()->getController()->redirect($redirection);
    }
  }

}

?>